<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
          <!-- 标题 -->
        <el-row
          ><el-col class="top_title">
            <span>说明：目前支持学科和关键字条件筛选</span>
            <el-button
              class="btn"
              type="success"
              size="small"
              icon="el-icon-edit"
              @click="$router.push('new')"
              >新增试题</el-button
            >
          </el-col></el-row
        >
        <!-- 顶部搜索表单 -->
        <el-form ref="addForm" :model="query" label-width="80px">
          <el-row>
            <!-- 渲染学科 -->
            <el-col :span="6">
              <el-form-item label="学科" prop="subjectID">
                <el-select
                  v-model="query.subjectID"
                  placeholder="请选择"
                  style="width: 100%"
                  @change="getsimpleChoose"
                >
                  <el-option
                    v-for="(item,index) in Subjects"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <!-- 渲染二级目录 -->
            <el-col :span="6">
              <el-form-item label="二级目录" prop="catalogID">
                <el-select
                  v-model="query.catalogID"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in catalogList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <!-- 渲染标签 -->
            <el-col :span="6">
              <el-form-item label="标签" prop="tags">
                <el-select
                  v-model="query.tags"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option v-for="(item,index) in tagsList"
                  :key="index"
                  :value="item.value"
                  :label="item.label"

                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <!-- 渲染关键字 -->
            <el-col :span="6">
              <el-form-item label="关键字" prop="keyword">
                <el-input
                  v-model="query.keyword"
                  placeholder="根据题干搜索"
                  style="width: 100%"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <!-- 渲染试题类型 -->
            <el-col :span="6">
              <el-form-item label="试题类型"  prop="questionType">
                <el-select
                  v-model="query.questionType"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option v-for="(item,index) in questionType" :key="index" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <!-- 渲染难度 -->
            <el-col :span="6">
              <el-form-item label="难度" prop="difficulty">
                <el-select
                  v-model="query.difficulty"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option v-for="(item,index) in difficulty" :key="index" :value="item.value" :label="item.label">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <!-- 渲染方向列表 -->
            <el-col :span="6">
              <el-form-item label="方向" prop="direction">
                <el-select
                  v-model="query.direction"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option v-for="(item,index) in direction" :key="index" :value="item">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <!-- 渲染录入人 -->
            <el-col :span="6">
              <el-form-item label="录入人" prop="creatorID">
                <el-select
                  v-model="query.creatorID"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option v-for="(item,index) in userList" :key="index" :value="item.username">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <!-- 渲染题目备注 -->
            <el-col :span="6">
              <el-form-item label="题目备注" prop="remarks">
                <el-input v-model="query.remarks" style="width: 100%"></el-input>
              </el-form-item>
            </el-col>

            <!-- 渲染企业简称 -->
            <el-col :span="6">
              <el-form-item label="企业简称"  prop="shortName">
                <el-input v-model="query.shortName" style="width: 100%"></el-input>
              </el-form-item>
            </el-col>

            <!-- 渲染城市下拉列表 -->
            <el-col :span="6">
              <el-form-item label="城市" prop="province">
                <el-select class="citys"
                  v-model="query.province"
                  placeholder="请选择"
                  style="width: 50%"
                  @change="citys(query.province)"
                >
                  <el-option v-for="(item,index) in provinceList" :key="index" :value="item">
                  </el-option>
                </el-select>
               <el-select
                  v-model="query.city"
                  placeholder="请选择"
                  style="width: 50%"
                >
                  <el-option v-for="(item,index) in citysList" :key="index" :value="item">
                  </el-option>
                </el-select>
              </el-form-item>
              </el-col>

            <!-- 渲染按钮 -->
          <el-col :span="6" style="text-align: right">
            <el-button size="small" @click="clear(query)">清除</el-button>
            <el-button size="small" type="primary" @click="search">
              搜索</el-button
            >
          </el-col>
          </el-row>
        </el-form>
        <!-- 中间数据 -->
        <el-alert
        type="info"
        show-icon
        :closable="false"
        :title="'数据一共' + counts + '条'"
      >
      </el-alert>

      <!-- 底部表格 -->
  <template>
    <el-table
      :data="questionList"
      style="width: 100%">
      <el-table-column
        prop="number"
        label="试题编号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="subject"
        label="学科"
        width="180">
      </el-table-column>
      <el-table-column
        prop="catalog"
        label="目录"
        width="180">
      </el-table-column>
       <el-table-column
        prop="questionType"
        label="题型"
        :formatter="formatterQuestionType"
        width="180"
        >
      </el-table-column>
       <el-table-column
        prop="question"
        label="题干"
        width="180">
        <template slot-scope="scope">
            <div v-html="scope.row.question"></div>
        </template>
      </el-table-column>
       <el-table-column
        prop="addDate"
        label="录入时间"
        width="180">
        <template slot-scope="scope">
           {{scope.row.addDate|parseTimeByString}}
        </template>
      </el-table-column>
       <el-table-column
        prop="difficulty"
        label="难度"
        :formatter="formatterDifficulty"
        width="180">
      </el-table-column>
       <el-table-column
        prop="creator"
        label="录入人"
        width="180">
      </el-table-column>
       <el-table-column
        fixed="right"
        prop="address"
        label="操作"
        width="180">
        <template slot-scope="scope">
            <el-button title="预览" size="small" plain circle type="primary" icon="el-icon-view" @click="opendialog(scope.row)"></el-button>
        <el-button title="修改"  size="small" plain type="success" icon="el-icon-edit" circle @click="$router.push('/questions/new?id=' + scope.row.id)"></el-button>
        <el-button title="删除"  size="small" plain type="danger" icon="el-icon-delete" circle @click="delQusertion(scope.row)"></el-button>
        <el-button title="加入精选" size="small" plain type="warning" icon="el-icon-star-off" circle @click="addQusertion(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </template>

  <!-- 分页功能 -->
    <el-pagination
     style="text-align: right; margin-top: 20px; margin-bottom: 15px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.page"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="query.pagesize"
      layout="sizes, prev, pager, next,jumper"
      :total="counts">
    </el-pagination>

 <!-- 预览弹框功能 -->
    <questionsPreview ref="child" :isvisible="isvisible"  @close="isvisible=false" :questionItem="questionItem"/>
      </el-card>
    </div>
  </div>
</template>

<script>

import { list, detail, choiceAdd, remove } from '@/api/hmmm/questions.js' // 基础题库
import questionsPreview from '../components/questions-preview' // 导入子组件
import { simple } from '@/api/hmmm/subjects.js' // 简单学科列表
import { simple as userSimple } from '@/api/base/users.js' // 录入人
import { provinces, citys } from '@/api/hmmm/citys.js'
import { simple as simplecatalog } from '@/api/hmmm/directorys' // 简单目录
import { simple as simpletags } from '@/api/hmmm/tags' // 简单标签
import { difficulty, questionType, direction } from '@/api/hmmm/constants'
export default {
  data () {
    return {
      isvisible: false, // 预览弹框
      difficulty, // 难度
      questionItem: {}, // 题库详情
      questionType, // 试题类型
      counts: 0,
      userList: [], // 录入人列表
      provinceList: [], // 省份列表
      citysList: [], // 地区列表
      getQuestionList: [], // 基础题库列表
      Subjects: [], // 学科列表
      catalogList: [], // 二级目录列表
      tagsList: [], // 标签列表
      questionList: [], // 题型列表
      direction, // 方向列表
      query: {
        page: 1, // 当前页数
        pagesize: 5, // 页尺寸
        subjectID: null, // 学科
        difficulty: '', // 难度
        questionType: null, // 试题类型
        tags: null, // 标签名称
        province: null, // 企业所在地省份
        city: null, // 企业所在城市
        keyword: null, // 关键字
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        direction: null, // 方向
        creatorID: null, // 录入人
        catalogID: null // 目录

      }
    }
  },
  components: { questionsPreview },
  created () {
    this.getQuestionlist() // 基础题库
    this.getSubjects()
    this.provinces()
    this.userSimple() // 录入人
  },
  methods: {
    // 加入精选
    async addQusertion (id) {
      const result = await this.$confirm('此操作将此题添加到精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (result === 'confirm') {
        const data = await choiceAdd(id)
        // 如果状态码是200表示添加成功
        if (data.status === 200) {
          this.$message.success('添加成功')
          this.getQuestionlist()
        }
      }
    },

    // 点击按钮出现预览弹框
    async opendialog (item) {
      const { data } = await detail({ id: item.id })
      this.questionItem = data
      //   console.log(this.questionItem)
      this.isvisible = true
    },

    // 当前页码发生改变时
    handleCurrentChange (val) {
      this.query.page = val
      this.getQuestionlist()
    },

    // 当每页条数发生改变时
    handleSizeChange (val) {
      this.query.pagesize = val
      this.getQuestionlist()
    },

    // 点击删除按钮,删除对应基础题库
    async delQusertion (item) {
      const confirmRes = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (confirmRes === 'confirm') {
        const data = await remove(item)
        // 如果状态码是200表示删除成功
        if (data.status === 200) {
          this.$message.success('删除成功')
          // 删除成功后刷新页面
          this.getQuestionlist()
        }
      }
    },

    // 点击清楚按钮,清楚表单内容

    clear () {
      this.$refs.addForm.resetFields()
      this.query.city = ''
    },

    // 点击搜索按钮
    async search () {
      const res = await list(this.query)
      //   console.log(res.data.items)
      this.questionList = res.data.items
    },

    // 格式化难度
    formatterDifficulty (row, colum, cellValue, index) {
      const obj = difficulty.find((item) => item.value === Number(cellValue))
      return obj ? obj.label : '未知'
    },

    // 格式化题型
    formatterQuestionType (row, column, cellValue, index) {
      const obj = questionType.find((item) => item.value === Number(cellValue))
      return obj ? obj.label : '未知'
    },

    // 获取录入人列表

    async userSimple () {
      const res = await userSimple()
      this.userList = res.data
    },

    // 当省份改变时 // 获取城市
    citys (item) {
      const res = citys(item)
      this.citysList = res
    },

    // 获取省份
    async provinces () {
      const res = await provinces()
      //   console.log(res)
      this.provinceList = res
    },

    // 根据学科获取学科id
    getsimpleChoose () {
      this.simplecatalog()
      this.simpletags()
    },

    // 根据学科ID获取catalog 二级目录
    async simplecatalog () {
      const res = await simplecatalog(this.query)
      this.catalogList = res.data
    },

    //  根据学科ID获取tags 标签
    async simpletags () {
      const res = await simpletags(this.query)
      this.tagsList = res.data
    },

    // 获取学科列表
    async getSubjects () {
      const res = await simple()
      this.Subjects = res.data
    //   console.log(this.Subjects)
    },

    // 获取基础题库列表
    async  getQuestionlist () {
      const { data: res } = await list(this.query)
      this.counts = res.counts
      //   console.log(res)
      this.questionList = res.items
    }
  }
}
</script>

<style scoped lang="scss">
.top_title {
  color: pink;
  font-size: 14px;
  margin-bottom: 10px;
}
.citys{
  padding-right:  10px;
}
.btn {
  float: right;
}
label {
  font-weight: 700;
  font-size: 14px;
}
.el-input__inner {
  width: 100%;
}
</style>
